package com.banksoft.cps.controller.admin;

import com.banksoft.cps.bean.Police;
import com.banksoft.cps.bean.User;
import com.banksoft.cps.bean.common.ErrorTip;
import com.banksoft.cps.bean.common.ServiceException;
import com.banksoft.cps.bean.query.PoliceQuery;
import com.banksoft.cps.controller.ControllerConstants;
import com.banksoft.cps.enums.UserType;
import com.banksoft.cps.service.PoliceService;
import com.banksoft.cps.util.CookieUtil;
import com.banksoft.cps.util.PageUtils;
import com.banksoft.cps.util.StringUtil;
import com.banksoft.cps.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by rongzhao.zhu on 2018/11/12.
 */
@Controller
@RequestMapping("/admin/police/")
public class AdminPoliceController extends ControllerConstants {
    @Autowired
    PoliceService policeService;
    @RequestMapping(value = "list", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String list(PoliceQuery query){
        List<Police> list =  policeService.list(query);
        return toJSONString(createDataTip(list));
    }
    @RequestMapping(value = "count", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String count(PoliceQuery query){
        long count =  policeService.count(query);
        long pageCount = PageUtils.getPage(count,query.getSize());
        return toJSONString(createDataTip(pageCount));
    }
    @RequestMapping(value = "find",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String find(String id){
        Police bean = policeService.find(id);
        return toJSONString(createDataTip(bean));
    }
    @RequestMapping(value = "save", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String save(Police bean, HttpServletRequest request){
        try {

            policeService.save(bean);

            return toJSONString(SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return toExceptionString(e);
        }
    }

    @RequestMapping(value = "setAccountAndPassword", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String setAccountAndPassword(Police bean, HttpServletRequest request){
        try {

            policeService.setAccountAndPassword(bean);

            return toJSONString(SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return toExceptionString(e);
        }
    }

    @RequestMapping(value = "delete", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String delete(String ids){
        if(StringUtil.isNullOrEmpty(ids)){
            return toJSONString(new ErrorTip(1,"请选择要删除的记录"));
        }
        policeService.delete(ids.split(","));
        return toJSONString(SUCCESS);
    }
    @RequestMapping(value = "loginOn", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String login(String account, String password, HttpServletRequest request, HttpServletResponse response){
        Police user = null;
        try{
            user =  policeService.login(account,password);
            CookieUtil.setLoginCookie(response,user,"");
        }catch (ServiceException e){
            return toExceptionString(e);
        }
        return toJSONString(createDataTip(user));
    }
}
